<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="测站名称" prop="stCd">
        <el-select v-model="queryParams.stCd" clearable filterable placeholder="请选择测站">
          <el-option v-for="item in allStationList"
                     :key="item.stCd" :label="item.stName" :value="item.stCd">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="功能码" prop="funcCode">
        <el-input
          v-model="queryParams.funcCode"
          placeholder="请输入功能码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="解析组名称" prop="analysisGroupId">
        <el-select v-model="queryParams.analysisGroupId" clearable filterable placeholder="请选择解析元素组">
          <el-option v-for="item in allElementGroupList"
                     :key="item.id" :label="item.analysisGroupName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="false"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="page" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="测站名称" prop="stInfoConfigEntity.stName" :show-overflow-tooltip="true" width="300"/>
      <el-table-column label="功能码" prop="funcCode" :show-overflow-tooltip="true" width="300"/>
      <el-table-column label="解析组名称" prop="elementAnalysisGroupConfigEntity.analysisGroupName"
                       :show-overflow-tooltip="true" width="500"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getPage"
    />

    <!-- 添加或修改解析元素对话框 -->
    <el-dialog :visible.sync="open" :title="title" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="180px">
        <el-form-item label="测站" prop="stCd">
          <el-select v-model="form.stCd" clearable filterable placeholder="请选择测站">
            <el-option v-for="item in allStationList"
                       :key="item.stCd" :label="item.stName" :value="item.stCd">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="解析元素组" prop="analysisGroupId">
          <el-select v-model="form.analysisGroupId" clearable filterable placeholder="请选择解析元素组">
            <el-option v-for="item in allElementGroupList"
                       :key="item.id" :label="item.analysisGroupName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="funcCode">
          <span slot="label">
            <el-tooltip content="针对水资源协议此处功能码为：控制域C加应用层功能码AFN，中间用英文逗号分隔" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            功能码
          </span>
          <el-input v-model="form.funcCode" placeholder="请输入报文中功能码" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    page,
    getStGroup,
    insertStGroup,
    updateStGroup,
    delStGroup,
  } from "@/api/analysisconfigure/stanalysisgroup";
  import {
    getAllStation
  } from "@/api/analysisconfigure/stationaccess";
  import {
    getAllElementGroup
  } from "@/api/analysisconfigure/elementgroup";


  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        page: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {
          identifierMsg: undefined,
          identifierValue: undefined,
          codingFormat: '',
          integerBit: undefined,
          radixPointBit: undefined,
          tableName: undefined,
          fieldName: undefined,
          fieldUnit: undefined,
        },
        dialogType: '',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          funcCode: undefined,
          stCd: undefined,
          analysisGroupId: undefined,
        },
        // 表单校验
        rules: {
          stCd: [{
            required: true,
            message: '请输入测站编码',
            trigger: 'blur'
          }],
          funcCode: [{
            required: true,
            message: '请输入报文中功能码',
            trigger: 'blur'
          }],
          analysisGroupId: [{
            required: true,
            message: '请输入解析元素组编码',
            trigger: 'blur'
          }]
        },
        allStationList: [],
        allElementGroupList: []
      };
    },
    created() {
      this.getPage();
      this.getAllStation();
      this.getAllElementGroup();
    },
    methods: {
      /** 查询分页列表 */
      getPage() {
        this.loading = true;
        page(this.queryParams).then(response => {
            this.page = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          identifierMsg: undefined,
          identifierValue: undefined,
          codingFormat: '',
          integerBit: undefined,
          radixPointBit: undefined,
          tableName: undefined,
          fieldName: undefined,
          fieldUnit: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getPage();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.title = "新增测站与解析组关联关系";
        this.open = true;
        this.dialogType = "add";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getStGroup(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改测站与解析组关联关系";
          this.dialogType = "update";
        });
      },
      /** 查询所有接入测站列表 */
      getAllStation() {
        getAllStation().then(response => {
          this.allStationList = response.data;
        });
      },
      /** 查询所有接入解析组列表 */
      getAllElementGroup() {
        getAllElementGroup().then(response => {
          this.allElementGroupList = response.data;
        });
      },

      /** 提交按钮 */
      submitForm: function () {
        let that = this;
        that.$refs["form"].validate(valid => {
          if (valid) {
            if ('update' === that.dialogType) {
              updateStGroup(that.form).then(response => {
                that.$modal.msgSuccess("修改成功");
                that.open = false;
                that.getPage();
              });
            } else {
              insertStGroup(that.form).then(response => {
                that.$modal.msgSuccess("新增成功");
                that.open = false;
                that.getPage();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.id || this.ids;
        this.$modal.confirm('是否确认删除？').then(function () {
          return delStGroup(id);
        }).then(() => {
          this.getPage();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
